package org.app.Service.Impl;

import jakarta.annotation.Resource;
import org.app.Entity.Admin;
import org.app.Entity.comm.Result;
import org.app.Mapper.Admin.IAdminMapper;
import org.app.Service.IAdminService;
import org.app.Utils.JwtUtil;
import org.springframework.stereotype.Service;

@Service
public class AdminService implements IAdminService {
    @Resource
    private IAdminMapper adminMapper;

    @Override
    public Result<Admin> adminLogin(String admin_phone, String admin_password){
        Result<Admin> result = new Result<>();
        if (adminMapper.getAdminByIDAndPassword(admin_phone,admin_password) != null){
            Admin admin = adminMapper.getAdminByIDAndPassword(admin_phone,admin_password);
            result.setCode(200);
            result.setMessage("登陆成功");

            String token = JwtUtil.getAdminToken(admin);
            admin.setToken(token);
            adminMapper.saveToken(admin);

            result.setData(admin);
        }else {
            result.setData(null);
            result.setCode(500);
            result.setMessage("登陆失败");
        }
        return result;
    }
    @Override
    public Result<Integer> adminRegister(Admin admin){
        Result<Integer> result = new Result<>();
        String phone = admin.getAdmin_phone();
        if(adminMapper.getAdminByPhone(phone) == null){
            result.setCode(500);
            result.setMessage("用户已存在");
            result.setCode(0);
        } else {
            result.setData(adminMapper.createAdmin(admin));
            if(result.getData() == 1){
                result.setCode(200);
                result.setMessage("注册成功");
            }else {
                result.setCode(500);
                result.setMessage("注册失败");
            }
        }
        return result;
    }
    @Override
    public Result<Integer> updateAdminInfo(Admin admin){
        Result<Integer> result = new Result<>();
        String admin_id = admin.getAdmin_id();
        if(adminMapper.selectById(admin_id)!=null){
            result.setData(adminMapper.updateAdminInfo(admin));
            if(result.getData() != null){
                result.setCode(200);
                result.setMessage("通过");
            }else {
                result.setCode(500);
                result.setMessage("有错误");
            }
        } else {
            result.setCode(500);
            result.setMessage("未能找到用户");
            result.setData(0);
        }
        return result;
    }
    @Override
    public Result<Integer> updateAdminPassword(String admin_id,String old_password,String new_password){
        Admin admin = adminMapper.selectById(admin_id);
        Result<Integer> result = new Result<>();

        if(admin != null){
            if(admin.getAdmin_password().equals(old_password)){
                admin.setAdmin_password(new_password);
            } else {
                result.setCode(500);
                result.setData(0);
                result.setMessage("原密码错误");
                return result;
            }
        }else {
            result.setCode(500);
            result.setData(0);
            result.setMessage("没有此用户");
            return result;
        }
        result.setData(adminMapper.updateAdminPassword(admin));
        if(result.getData() != null){
            result.setCode(200);
            result.setMessage("设置密码成功");
        }else {
            result.setCode(500);
            result.setMessage("更新密码失败");
        }
        return result;
    }
    @Override
    public Result<Integer> forgetPassword(String admin_id,String new_password){
        Result<Integer> result = new Result<>();
        Admin admin = adminMapper.selectById(admin_id);
        admin.setAdmin_password(new_password);
        Integer code = adminMapper.updateAdminPassword(admin);
        if(code == 0){
            result.setCode(500);
            result.setMessage("出错了");
            result.setData(0);
        } else {
            result.setCode(200);
            result.setData(1);
            result.setMessage("修改成功");
        }
        return result;
    }
    public Result<Admin> getAdminByPhone(String admin_phone){
        Result<Admin> result = new Result<>();
        if(adminMapper.getAdminByPhone(admin_phone) != null){
            result.setCode(200);
            result.setData(adminMapper.getAdminByPhone(admin_phone));
            result.setMessage("已找到");
        } else {
            result.setCode(500);
            result.setData(null);
            result.setMessage("未能找到用户");
        }
        return result;

    }

}
